<?php
class pager {
    // Khai thuoc tinh cho lop
        var $url = NULL;
        var $total_records = NULL;
        var $scroll_page = NULL;
        var $per_page = NULL;
        var $current_page = NULL;
        var $start = NULL ;
        var $total_pages = NULL; // Thuoc tinh tong so trang
/*?*/   var $page_links = NULL;
        var $previous_page = NULL;
        var $next_page = NULL;
/*?*/   var $firs_page = NULL;
/*?*/   var $last_page = NULL;
	function total_pages($paper_url, $total_records, $scroll_page, $per_page, $current_page ){
        $this->url = $paper_url;
        $this->total_records = $total_records;
        $this->scroll_page = $scroll_page;
        $this->per_page = $per_page;
        if(!is_numeric($current_page)){
            $this->current_page = 1;
        }else{
            $this->current_page = $current_page;
        }
        if($this->current_page==1){
            $this->start = 0;
        }else{
            $this->start = ($this->current_page - 1) * $this->per_page;
        }
 /*edit*/       $this->total_pages = ceil($this->total_records / $this->per_page);        // TOng so page
	}
    // Function 2 : Thiet lap link cho cac nut chuyen trang
	function page_links($inactive_page_tag,$pager_url_last){
        if($this->total_pages <= $this->scroll_page ){
	    if($this->total_records <= $this->per_page){
                $loop_start = 1;
                $loop_finish = $this->total_pages;
            }else{
                $loop_start = 1;
                $loop_finish = $this->total_pages;
            }
        }else{
            if($this->current_page < intval(($this->scroll_page/2) + 1)){
                $loop_start = 1;
                $loop_finish = $this->scroll_page;
            }else{
                $loop_start = $this->current_page - intval($this->scroll_page/2);
                $loop_finish = $this->current_page + intval($this->scroll_page/2);
                if($loop_finish > $this->total_pages){
                    $loop_finish = $this->total_pages;
                }
            }
        } //<!-- End if -->
        for($i = $loop_start;$i <= $loop_finish;$i++){
            if($i == $this->current_page){
                $this->page_links.='<span '.$inactive_page_tag.'>'.$i.'</span>';
            }
        else{
                $this->page_links.='<span id="link_dif"><a href="'.$this->url.$i.$pager_url_last.'">'.$i.'</a></span>';
        }
       }
    }   //<!-- End function -->

    //Function 3  Nut trang sau
    function previous_page($previous_page_text, $pager_url_last){
        if($this ->current_page > 1 ){
            $this->previous_page = '<span class=prev_page><a href = "'.$this->url.($this->current_page - 1).$pager_url_last.'">'
            .$previous_page_text.'</a></span>';

        }
    }

    //Function 4 Nut trang sau
    function next_page($next_page_text, $pager_url_last){
        if($this->current_page < $this->total_pages ){
            $this->next_page = '<span class=next_page><a href = "'.$this->url.($this->current_page + 1).$pager_url_last.'">'
            .$next_page_text.'</a></span>';

        }
    }

    //Function Nut trang dau
    function firs_page($first_page_text, $pager_url_last){
        if($this ->current_page > 1 ){
            $this->firs_page = '<span class=first_page><a href = "'.$this->url.'1'.$pager_url_last.'">'.$first_page_text.'</a></span>';

        }
    }

    //Function : Nut trang cuoi
    function last_page($last_page_text, $pager_url_last){
        if($this ->current_page > 1 ){
            $this->last_page = '<span class=last_page><a href = "'.$this->url.$this->total_pages.$pager_url_last.'">'.$last_page_text.
            '</a></span>';
        }
    }

    //  Set cac gia tri cho function co trong class
    function paper_set($paper_url, $total_records, $scroll_page, $per_page, $current_page,$inactive_page_tag,
    $previous_page_text,$next_page_text,$first_page_text,$last_page_text, $pager_url_last=''){
        $this->total_pages($paper_url, $total_records, $scroll_page, $per_page, $current_page );
        $this->page_links ($inactive_page_tag,$pager_url_last);
        $this->previous_page($previous_page_text, $pager_url_last);
        $this->next_page($next_page_text, $pager_url_last);
        $this->firs_page($first_page_text, $pager_url_last);
        $this->last_page($last_page_text, $pager_url_last);
    }

}  //<!-- End class-->

class getDivPage{
    public function getDataToDivPage(pager $page,$count,$url,$sobanghi)
    {
        //$stg = new caidatcontroller();
        $total_records = count($count);
        $scroll_page = 5;
        $per_page = $sobanghi ;
        $current_page = isset($_GET['page'])?$_GET['page']:1;
        $paper_url =$url;
        $inactive_page_tag = 'id="current_page"';
        $previous_page_text = '<';
        $next_page_text = '>';
        $first_page_text = '<<';
        $last_page_text = '>>';
        $page->paper_set($paper_url, $total_records, $scroll_page, $per_page, $current_page,$inactive_page_tag,$previous_page_text,
        $next_page_text,$first_page_text,$last_page_text);
        $dau = $page->start;
        $cuoi = $page->per_page;
        $arr = array(
            "begin" => $dau,
            "end" => $cuoi
        );
        return $arr;
    }
}
?>